package com.suxin.plugin.intergration.factory;

import com.suxin.plugin.intergration.factory.process.PluginPipeProcessorFactory;
import com.suxin.plugin.intergration.factory.process.PluginPostProcessorFactory;
import com.suxin.plugin.intergration.factory.process.pip.PluginPipeProcessor;
import com.suxin.plugin.intergration.factory.process.post.PluginPostProcessor;
import com.suxin.plugin.intergration.manager.PluginWrapper;
import com.suxin.plugin.intergration.manager.plugin.PluginManager;
import com.suxin.plugin.listener.PluginListener;
import com.suxin.plugin.listener.PluginListenerFactory;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Tang
 * @classname DefaultPluginFactory
 * @description [  ]
 * @date 2022/7/18 21:42
 */
public class DefaultPluginFactory implements PluginFactory {

    private PluginManager pluginManager;
    private PluginListenerFactory listenerFactory;
    private ApplicationContext applicationContext;

    /**
     * 注册的插件的集合
     */
    private final Map<String, PluginRegistryInfo> registryInfoMap = new HashMap<>();
    private final List<PluginRegistryInfo> buildContainer = new ArrayList<>();

    private final PluginPostProcessor pluginPostProcessor;
    private final PluginPipeProcessor pluginPipeProcessor;

    public DefaultPluginFactory(PluginManager pluginManager, PluginListenerFactory listenerFactory,
                                ApplicationContext applicationContext) {
        this.pluginManager = pluginManager;
        this.listenerFactory = listenerFactory;
        this.applicationContext = applicationContext;

        // 初始化处理链
        this.pluginPipeProcessor = new PluginPipeProcessorFactory(this.applicationContext);
        this.pluginPostProcessor = new PluginPostProcessorFactory(this.applicationContext);
    }


    @Override
    public void initialize() throws Exception {
        this.pluginPipeProcessor.initialize();
        this.pluginPostProcessor.initialize();
    }

    @Override
    public PluginFactory registry(PluginWrapper pluginWrapper) throws Exception {
        PluginRegistryInfo pluginRegistryInfo = new PluginRegistryInfo(pluginWrapper);
        // pluginPipeProcessor 链执行
        this.pluginPipeProcessor.registry(pluginRegistryInfo);
        this.registryInfoMap.put(pluginRegistryInfo.getPluginId(), pluginRegistryInfo);
        buildContainer.add(pluginRegistryInfo);
        return this;
    }

    @Override
    public PluginFactory unRegistry(String pluginId) throws Exception {
        return this;
    }

    @Override
    public void build() throws Exception {
        this.pluginPostProcessor.registry(buildContainer);
    }

    @Override
    public void addListener(PluginListener pluginListener) {

    }

    @Override
    public <T extends PluginListener> void addListener(Class<T> pluginListener) {

    }

    @Override
    public void addListener(List<PluginListener> pluginListeners) {

    }
}
